package org.rooster.chromosomeevaluator.rules;

import org.jgap.Gene;
import org.jgap.IChromosome;
import org.rooster.genetic.GenericRule;
import org.rooster.manager.ResourceManager;

import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: manorama
 * Date: 6/5/12
 * Time: 9:08 PM
 * To change this template use File | Settings | File Templates.
 */
public class EmployeeNotWithinMinMaxRangePenaltyRule implements GenericRule{

    private int weight = 1;
    private ArrayList<int[]> ruleViolations = new ArrayList<int[]>();

    public EmployeeNotWithinMinMaxRangePenaltyRule() {
    }

    public EmployeeNotWithinMinMaxRangePenaltyRule(int weight) {
        this.weight = weight;
    }

    /**
     * If the employee is assigned to work within minimum and maximum working time slots range
     * this method adds a bonus value for it
     * @param chromosome
     * @return  the total bonus value
     */

    @Override
    public double fitnessValueForRule(IChromosome chromosome) {
        int penalty = 1;
        int [] workingSlots = new int[ResourceManager.TOTAL_NO_OF_EMPLOYEES];
        for (int i=0; i<workingSlots.length; i++) {
            workingSlots[i] = 0;
        }
        Gene[] genes = chromosome.getGenes();
        for(int i=0; i<genes.length; i++) {
            workingSlots[(Integer)genes[i].getAllele() - 1] += 1;
        }
        for(int i=0; i<workingSlots.length; i++) {
            if(workingSlots[i] < ResourceManager.MIN_WORKING_SLOTS || workingSlots[i] > ResourceManager.MAX_WORKING_SLOTS) {
                penalty++;
                ruleViolations.add(new int[]{i,workingSlots[i]});
            }

        }
        
        return 1.0/(penalty * weight);
    }

    @Override
    public int getWeight() {
        return weight;
    }

    @Override
    public void setWeight(int value) {
        this.weight = value;
    }

    /**
     * @return the ruleViolations
     */
    public ArrayList<int[]> getRuleViolations() {
        return ruleViolations;
    }
}
